package org.windowkit.toolkit.util;

import java.awt.Insets;
import java.net.URL;

import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

/**
 * Util methods for creating common components.
 *
 * TODO: this class was ripped from sourcemanager - needs to be made more general for it to be usefull
 * @author Rich O'Connell
 */
public class ComponentUtils {

    /**
     * Utility method to create a non floatable toolbar with
     * a button for each of the given actions.
     *
     * @param actions
     * @return
     */
    public static JToolBar createToolbar(final Action ...actions){
        JToolBar ret = new JToolBar();
        ret.setFloatable(false);
        createToolbar(ret, actions);
        return ret;
    }
    public static JToolBar createToolbar(final Insets buttonInsets, final boolean showText, final Action ...actions){
    	JToolBar ret = new JToolBar();
    	ret.setFloatable(false);
    	createToolbar(ret, buttonInsets, showText, actions);
    	return ret;
    }

    /**
     * Utility method that will add the actions to the toolbar
     * as buttons.
     *
     * @param ret
     * @param actions
     * @return
     */
    public static void createToolbar(final JToolBar tb, final Action ...actions){
    	createToolbar(tb, new Insets(0, 0, 0,0), false, actions);
    }

    public static void createToolbar(final JToolBar tb, final Insets buttonInsets, final boolean showText, final Action ...actions){
        for(Action action : actions){
            tb.add(createToolbarButton(action, buttonInsets, showText));
            tb.add(Box.createHorizontalStrut(2));
        }
    }
    /**
     * Utility method that creates a toolbar button using the given Action.
     * The text is hidden and the margins are shrunk.
     *
     * @param action
     * @return
     */
    public static JButton createToolbarButton(final Action action, final Insets insets, final boolean showText){
        JButton ret =  new JButton();
        if (!showText) {
        	ret.putClientProperty("hideActionText", true);
        }
        ret.setAction(action);
        ret.setMargin(insets);
        return ret;
    }

    /**
     * Utility method to configure an action with the given params.
     * @param action - The action to configure.
     * @param name - THe name to set.
     * @param shortDesc - The short description to set.
     * @param iconLoc - The name of the file to use for the icon.
     */
    public static Action configureAction(final Action action, final String name, final String shortDesc, final String iconLoc){
    	return configureAction(action, name, shortDesc, createImageIcon(name));

    }
    /**
     * Utility method to configure an action with the given params.
     * @param action action to configure
     * @param name name of the action
     * @param shortDesc short description
     * @param icon The icon for the action
     * @return
     */
    public static Action configureAction(final Action action, final String name, final String shortDesc, final ImageIcon icon){
        if(name != null) {
			action.putValue(Action.NAME, name);
		}
        if(icon != null) {
			action.putValue(Action.SMALL_ICON, icon);
		}
        if(shortDesc != null) {
			action.putValue(Action.SHORT_DESCRIPTION, shortDesc);
		}
        return action;
    }


    /**
     * Utility method to create an ImageIcon from the image at the given location in the class path.
     * @param gifName
     * @return
     */
    public static ImageIcon createImageIcon(final String gifName){
        URL imgLoc = ComponentUtils.class.getResource(gifName);
        ImageIcon ii = new ImageIcon(imgLoc);
        return ii;
    }

}
